//
//  DXWAddChannelController.m
//  pandaTV
//
//  Created by 淡馨慰 on 16/10/18.
//  Copyright © 2016年 淡馨慰. All rights reserved.
//

#import "DXWAddChannelController.h"

#import <MJExtension/MJExtension.h>
#import "YZTagList.h"

#import "DXWChannelItem.h"
#import "DXWChannelGroupItem.h"

#import "DXWOftenChannelCell.h"
#import "DXWAllChannelCell.h"

static NSString * const oftenCell = @"oftenCell";
static NSString * const allCell = @"allCell";

@interface DXWAddChannelController ()<UICollectionViewDelegate>

/** 网络请求 --- 连播图片模型数组 */
@property (nonatomic, strong) NSMutableArray *channels;

/** 网络请求 --- 连播图片模型数组 */
@property (nonatomic, strong) NSMutableArray *selChannels;

/** 网络请求字典 */
@property (nonatomic, strong) DXWChannelGroupItem *groupItem;
/** 选中的标签列表 */

/** 可以移动的标签View */
@property (nonatomic, strong) YZTagList *tagList;

@property (nonatomic, strong) NSMutableDictionary *selectTagDict;
@end

@implementation DXWAddChannelController

- (DXWChannelGroupItem *)groupItem
{
    if (_groupItem == nil) {
        _groupItem = [[DXWChannelGroupItem  alloc]init];
    }
    return _groupItem;
}
-(NSMutableArray *)channels
{
    if (_channels == nil) {
        _channels = [NSMutableArray array];
    }
    return _channels;
}
-(NSMutableArray *)selChannels
{
    if (_selChannels == nil) {
        _selChannels = [NSMutableArray array];
    }
    return _selChannels;
    
}
#pragma mark -YJ框架
- (YZTagList *)tagList
{
    if (_tagList == nil) {
        _tagList = [[YZTagList alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 0)];

        __weak typeof(self) weakSelf = self;
        
        _tagList.clickTagBlock = ^(NSString *tag){
            [weakSelf clickTag:tag];
            
        };
        _tagList.tagColor = [UIColor whiteColor];
        NSArray *tags = @[@"英雄联盟",@"阴阳师",@"英雄联盟1",@"阴阳师1",@"英雄联盟2",@"阴阳师2"];
        
        // 设置排序时，缩放比例
        _tagList.scaleTagInSort = 1.2;
        // 需要排序
        _tagList.isSort = YES;
        // 标签尺寸
        NSInteger col = 3;
        _tagList.tagSize = CGSizeMake(tagW, tagH);
        // 不需要自适应标签列表高度
        _tagList.isFitTagListH = NO;
        // 设置标签字体大小
        _tagList.tagFont = tag_Font;
        // 设置标签背景图片
        _tagList.tagBackgroundImage = [UIImage imageNamed:@"homepage_refresh_button_dis"];

        // 设置标签内字体颜色
        _tagList.tagColor = [UIColor whiteColor];

        /**
         *  这里一定先设置标签列表属性，然后最后去添加标签
         */
        [_tagList addTags:tags];
    }
    return _tagList;
}


- (void)viewDidLoad {
    [super viewDidLoad];
     [self loadData_channel];
    self.navigationItem.title = @"频道选择";
    self.tableView.backgroundColor = dxw_MainColor;


    
    // 注册常用频道cell
        [self.tableView registerClass:[DXWOftenChannelCell class] forCellReuseIdentifier:oftenCell];

    [self.tableView registerNib:[UINib nibWithNibName:@"DXWAllChannelCell" bundle:nil] forCellReuseIdentifier:allCell];

//    [self setupTag]; DXWAllChannelCell
        [self.tableView reloadData];
}
- (void)clickTag:(NSString *)tag
{
    // 删除标签
    [_tagList deleteTag:tag];
    
    // 刷新第0组
    NSIndexSet *indexSex = [NSIndexSet indexSetWithIndex:0];
    [self.tableView reloadSections:indexSex withRowAnimation:UITableViewRowAnimationNone];
    
    // 更新cell标题
//    YZTagCell *cell = self.selectTagDict[tag];
//    YZTagItem *item = cell.item;
//    item.isSelected = !item.isSelected;
//    cell.item = item;
    
    // 移除选中的cell
    [self.selectTagDict removeObjectForKey:tag];
}

#pragma mark -tableView DataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        DXWOftenChannelCell *cell = [tableView dequeueReusableCellWithIdentifier:oftenCell];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
//        if (cell == nil) {
//            cell = [[DXWOftenChannelCell alloc]init];
//        }
        cell.tagList.frame = cell.bounds;
        cell.tagList = self.tagList;
        return cell;
    }
    DXWAllChannelCell *cell = [tableView dequeueReusableCellWithIdentifier:allCell];
    if (!cell) {
        cell = [DXWAllChannelCell allChannelCell];
    }
    cell.bounds = CGRectMake(0, 0, screenW, screenH);
    cell.collectionView.frame = cell.bounds;
    cell.collectionView.delegate = self;
    cell.collectionView.backgroundColor = [UIColor whiteColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
//    NSLog(@"%@",_channels);
    
    cell.channels = _groupItem.data;

//    NSLog(@"%@",self.channels[indexPath.row]);
//     if (self.channels[indexPath.row] == nil) {
//        [self loadData_channel];
//        [self.tableView reloadData];
//    }
 
//    groupItem.data = self.channels[indexPath.row];
//    cell.groupItem = groupItem;
    
    return cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 150;
    }
//    DXWChannelGroupItem *groupItem = _groupItem.data[indexPath.row];
    return _groupItem.cellH ;

//        return screenH;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
      NSLog(@"%ld",indexPath.row);
}
#pragma mark -UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"%ld",indexPath.row);
    NSLog(@"标签总数%ld",_groupItem.data.count);
    DXWChannelItem *item = _groupItem.data[indexPath.row];
    NSLog(@"%@",item);
    [_selChannels addObject:item];
    [_groupItem.data removeObjectAtIndex:indexPath.row - 1];
    NSLog(@"删除后%ld",_groupItem.data.count);
    
//    DXWChannelGroupItem *groupitem = cell.groupItem;
//    groupitem.isSelected = !item.isSelected;
//    NSLog(@"%@",cell.groupItem);
//    cell.item = item;
}





#pragma mark -加载网络数据
- (void)loadData_channel
{
    /*
     
     添加频道 http://api.m.panda.tv/ajax_get_all_subcate?__version=2.0.2.1350&__plat=ios&__channel=appstore
     
     */
    AFHTTPSessionManager *manager = [AFHTTPSessionManager dxw_HTTPManager];
    NSDictionary *dict = @{
                           @"__version" : @"2.0.2.1350",
                           @"__plat" : @"ios",
                           @"__channel" : @"appstore"
                           };
    [manager GET:@"http://api.m.panda.tv/ajax_get_all_subcate?" parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        self.channels = [DXWChannelGroupItem  mj_objectArrayWithKeyValuesArray:responseObject];
       self.groupItem = [DXWChannelGroupItem mj_objectWithKeyValues:responseObject];
        NSLog(@"%@------",_groupItem);
        //处理缺口
        [self.tableView reloadData];
        
//        [responseObject writeToFile:@"/Users/milkteacat/Desktop/熊猫TV/首页_频道1.plist" atomically:YES];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
    
}

@end
